package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TemplateServcie;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemCatController")
public class ItemCatController {
    //开启日志
    Logger logger = LoggerFactory.getLogger(TemplateController.class);

    @Reference(check=false ,timeout=106666)
    private ItemCatService itemCatService;
    @Reference(check=false ,timeout=106666)
    private TemplateServcie templateServcie;

    //跳转到分类管理页面
    @RequestMapping("toItemCatList")
    public String toItemCatList(Model model){
        List<TbTypeTemplate> template = templateServcie.getTemplateAllInfo();
        model.addAttribute("template",template);
        return "admin/item_cat";
    }

    //获取分类信息
    @RequestMapping("getItemCatInfo")
    @ResponseBody
    public Result getItemCatInfo(@RequestParam(defaultValue = "0") Integer parentId){
        try {
            logger.info("查询方法开始运行{}");
            List<TbItemCat> itemCatPageBean = itemCatService.getItemCatInfo(parentId);
            return Result.response(ResultEnum.SUCCESS).setData(itemCatPageBean);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //修改分类信息回显
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(Integer id){
        try {
            logger.info("回显方法开始运行{}");
            TbItemCat itemCat = itemCatService.updateInfo(id);
            return Result.response(ResultEnum.SUCCESS).setData(itemCat);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("update")
    @ResponseBody
    public Result update(TbItemCat itemCat){
        try {
            logger.info("修改方法开始运行{}");
            itemCatService.update(itemCat);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //增加
    @RequestMapping("insertItemCat")
    @ResponseBody
    public Result insertItemCat(TbItemCat itemCat){
        try {
            logger.info("增加方法开始运行{}");
            itemCatService.insertItemCat(itemCat);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //批量删除
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]")Integer[] ids){
        try {
            logger.info("删除方法开始运行{}");
            itemCatService.deleteBatch(ids);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
}
